package com.cskaoyan.service.admin.storageobject;

import com.cskaoyan.bean.MarketStorage;
import com.cskaoyan.bean.MarketStorageExample;
import com.cskaoyan.bean.vo.admin.storageobject.StorageObjectListVo;
import com.cskaoyan.mapper.MarketStorageMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: bwj
 * @Date: 2022/09/08/17:46
 * @Description:
 */
@Service
public class StorageObjectServiceImpl implements StorageObjectService {
    @Autowired
    MarketStorageMapper marketStorageMapper;

    /**
    * @Author: 宾文杰 @Description: 显示全部图片信息 @Date: 2022/9/8
    */
    @Override
    public StorageObjectListVo objectList(Integer page, Integer limit, String key, String name, String sort, String order) {
        PageHelper.startPage(page,limit);
        MarketStorageExample marketStorageExample = new MarketStorageExample();
        marketStorageExample.setDistinct(true);
        marketStorageExample.setOrderByClause(sort+" "+order);
        MarketStorageExample.Criteria criteria = marketStorageExample.createCriteria();
        if (key!=null&&!StringUtils.isEmpty(key)){
            criteria.andKeyLike("%"+key+"%");
        }
        if (name!=null&&!StringUtils.isEmpty(name)){
            criteria.andNameLike("%"+name+"%");
        }
        List<MarketStorage> marketStorages = marketStorageMapper.selectByExample(marketStorageExample);
        StorageObjectListVo objectListVo = new StorageObjectListVo();
        objectListVo.setLimit(limit);
        objectListVo.setPage(page);
        PageInfo pageInfo = new PageInfo(marketStorages);
        objectListVo.setPages(pageInfo.getPages());
        objectListVo.setTotal((int) pageInfo.getTotal());
        objectListVo.setList(marketStorages.toArray());
        return objectListVo;
    }

    /**
    * @Author: 宾文杰 @Description: 更新对象信息 @Date: 2022/9/8
    */
    @Override
    public MarketStorage objectUpdate(MarketStorage marketStorage) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(new Date());
        try {
            Date updateTime = dateFormat.parse(format);
            marketStorage.setUpdateTime(updateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        marketStorageMapper.updateByPrimaryKey(marketStorage);
        MarketStorage marketStorage1 = marketStorageMapper.selectByPrimaryKey(marketStorage.getId());
        return marketStorage1;
    }

    /**
    * @Author: 宾文杰 @Description: 删除对象信息 @Date: 2022/9/8
    */
    @Override
    public void objectDelete(MarketStorage marketStorage) {
        marketStorageMapper.deleteByPrimaryKey(marketStorage.getId());
    }
}
